package cn.edu.hbvtc.qms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.edu.hbvtc.qms.domain.Department;
import cn.edu.hbvtc.qms.domain.Major;
import cn.edu.hbvtc.qms.domain.Role;
import cn.edu.hbvtc.qms.domain.Teacher;
import cn.edu.hbvtc.qms.domain.User;
import cn.edu.hbvtc.qms.service.IManagementService;
import cn.edu.hbvtc.qms.web.struts.form.MajorActionForm;

public class MajorAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到专业管理列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得当前登录用户
		User user = managementService.findUserByUsername(this.getPrincipal().getUsername());
		Role role = managementService.findRoleByName("DEPT_ADMIN");
		
		if(user.getRoles().contains(role)){
			Teacher teacher = managementService.findTeacherByUserId(user.getId());
			List<Major> majorList = managementService.findMajorsOfDept(teacher.getDept());
			request.setAttribute("majorList", majorList);
		}else{
			List<Major> majorList = managementService.findMajors();
			request.setAttribute("majorList", majorList);
		}
		return mapping.findForward("list");
	}

	// 跳转到专业添加页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		User user = managementService.findUserByUsername(this.getPrincipal().getUsername());
		Role role = managementService.findRoleByName("DEPT_ADMIN");
		
		if(user.getRoles().contains(role)){
			Teacher teacher = managementService.findTeacherByUserId(user.getId());
			Department dept = teacher.getDept();
			request.setAttribute("dept", dept);
			
		}else{
			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);
		}
		return mapping.findForward("add");
	}

	// 完成到专业添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MajorActionForm maf = (MajorActionForm) form;
		
		//表单校验、
		if( maf.getMajorId() == null || "".equals(maf.getMajorId())){
			request.setAttribute("majorIdError","请输入专业编号");
			
			return mapping.findForward("toAdd");
		}
		else if(managementService.findMajorByMajorId(maf.getMajorId()) !=null){
			request.setAttribute("majorIdRequest","您输入的专业编号已存在，请重新输入！");
			
			return mapping.findForward("toAdd");
		}
		if(maf.getMajorName() == null || "".equals(maf.getMajorName())){
			request.setAttribute("majorNameError","请输入专业名称");
			
			return mapping.findForward("toAdd");
		}
		if(maf.getDeptId() == 0){
			request.setAttribute("deptIdError", "请输入所属院系！");
			
			return mapping.findForward("toAdd");
		}
		
		Major major = new Major();
		PropertyUtils.copyProperties(major, maf);
		
		Department dept = managementService.findDepartmentById(maf.getDeptId());
		major.setDept(dept);
		
		managementService.saveMajor(major);

		return mapping.findForward("toList");
	}

	// 跳转到专业修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		MajorActionForm maf = (MajorActionForm) form;
		Major major = managementService.findMajorById(maf.getId());
		request.setAttribute("major", major);

		//将默认值保存到ActionForm中，以便在JSP表单中显示该值。
		maf.setDeptId(major.getDept().getId());
		
		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);
		
		return mapping.findForward("update");
	}

	// 保存专业修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MajorActionForm maf = (MajorActionForm) form;
		
		//添加校验功能
		if(!maf.getMajorId().equals(managementService.findMajorById(maf.getId()).getMajorId())){//校验提交的专业编码是否修改
		
		if( maf.getMajorId() == null || "".equals(maf.getMajorId())){
			request.setAttribute("majorIdError","请输入专业编号");
			
			return mapping.findForward("toUpdate");
		}
		else if(managementService.findMajorByMajorId(maf.getMajorId()) !=null){
			request.setAttribute("majorIdRequest","您输入的专业编号已存在，请重新输入！");
			
			return mapping.findForward("toUpdate");
			}
		}
		if(maf.getMajorName() == null || "".equals(maf.getMajorName())){
			request.setAttribute("majorNameError","请输入专业名称");
			
			return mapping.findForward("toUpdate");
		}
		if(maf.getDeptId() == 0){
			request.setAttribute("deptIdError", "请输入所属院系！");
			
			return mapping.findForward("toUpdate");
		}
		
		Major major = managementService.findMajorById(maf.getId());
		PropertyUtils.copyProperties(major, maf);
		Department dept = managementService.findDepartmentById(maf.getDeptId());
		major.setDept(dept);
		managementService.updateMajor(major);

		return mapping.findForward("updateSave");
	}

	// 完成专业删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MajorActionForm maf = (MajorActionForm) form;
		managementService.deleteMajorById(maf.getId());

		List<Major> majorList = managementService.findMajors();
		request.setAttribute("majorList", majorList);

		return mapping.findForward("del");
	}
}
